package com.tensequare.search.controller;

import com.tensequare.search.pojo.Article;
import com.tensequare.search.service.ArticleService;
import common.entity.PageResult;
import common.entity.Result;
import common.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/3/26 16:04
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    //添加到索引库
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody  Article article){
        articleService.save(article);
        return new Result(true, StatusCode.OK,"添加到索引库成功！");
    }

    //文章搜索
    @RequestMapping(value = "/search/{keywords}/{page}/{size}",method = RequestMethod.GET)
    public Result search(@PathVariable String keywords,@PathVariable int page,@PathVariable int size){
        Page<Article> pageData = articleService.findByTitleOrContentLike(keywords,page,size);
        return new Result(true,StatusCode.OK,"查询成功",
                new PageResult<>(pageData.getTotalElements(),pageData.getContent()));
    }
}
